function [lambdat_hat, Vnt_hat, betat_hat, yt_hat, R2_t] = AJX_HF_tod(dFt_j, dPt_j, dPt_jp1,beta_J,tod,todj,nobs)
%    load('Data/beta_J.mat', 'beta_J');
    
    K         =     size(dFt_j,1);
    M         =     size(dPt_j,1);
    H         =     size(beta_J,2)-1;
    qn        =     size(dFt_j,2);
    un        =     3;
    Delta_n   =     1/252/(nobs-1);
    vn        =     1000*log(1/((24+5/12)*Delta_n));
    
    beta_J    =     beta_J(:,2:end);
    
    dPtj_NaN  =     (sum(isnan(dPt_j),2)>0);
    
    beta_J_hat_j  =   beta_J.*~dPtj_NaN;
    
    dPt_j(isnan(dPt_j)) = 0;
    dPt_jp1(isnan(dPt_jp1)) = 0;
    
    dFt_cont_ix_j = fn_cont_ix_tod(un, 0.47, dFt_j, Delta_n, qn/nobs, nobs,tod);
    dPt_cont_ix_j = fn_cont_ix_tod(un, 0.47, dPt_j, Delta_n, qn/nobs, nobs,todj);
    
    % Retain only continuous movements in factors
    dFt_j = dFt_j.*dFt_cont_ix_j;
    dPt_j = dPt_j.*dPt_cont_ix_j;
    
    
    cF_t    = 1/(qn*Delta_n) * dFt_j*(dFt_j');
    gamma_t = 1/(qn*Delta_n) * (dPt_j*dFt_j');
        
    if min(eig(cF_t)) > 1/vn
        beta_C_hat_j = gamma_t * inv(cF_t);
    else
        beta_C_hat_j = zeros(M,K);
    end
    
    for i = 1:M
        if dPtj_NaN(i) > 0
            beta_C_hat_j(i,:) = zeros(1,K);
        end
    end
    
    beta_hat = [beta_C_hat_j, beta_J_hat_j];
    
    b_hat = beta_hat' * beta_hat;

    if min(eig(b_hat)) > 1/vn
        eta_hat = inv(b_hat) * beta_hat';
    else
        eta_hat = zeros(K+H,M);
    end
    
    lambdat_hat = eta_hat * sum(dPt_jp1,2);
    R2_t        = (lambdat_hat'*(beta_hat'))*(lambdat_hat'*(beta_hat'))'/((sum(dPt_jp1,2)')*(sum(dPt_jp1,2)));
    Vnt_hat     = eta_hat * sum(dPt_jp1,2)*(sum(dPt_jp1,2)') * (eta_hat');
    betat_hat   = beta_hat;
    yt_hat      = sum(dPt_jp1,2);

end
